package com.tyqx.hashchainbiz.service.impl;

// import com.alibaba.fastjson.JSON;
import com.tyqx.hashchainbean.module.HashIndex;
import com.tyqx.hashchainbean.module.JZTAttachmentBusiness;
import com.tyqx.hashchainbean.module.NoHashValueJson;
import com.tyqx.hashchainbean.vo.JZTAttachmentBusinessVO;
import com.tyqx.hashchainbiz.service.JZTAttachmentBusinessService;
import com.tyqx.hashchaindao.mapper.HashIndexMapper;
import com.tyqx.hashchaindao.mapper.JZTAttachmentBusinessMapper;
import com.tyqx.hashchaindao.mapper.NoHashValueJsonMapper;
import com.tyqx.hashchainutil.common.HashValueUtil;
import com.tyqx.hashchainutil.common.mongodb.ImagePathHashValueUtil;
import hashchainutil.common.EmptyUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;

@Service("jztAttachmentBusinessService")
public class JZTAttachmentBusinessServiceImpl implements JZTAttachmentBusinessService {

    @Resource
    private JZTAttachmentBusinessMapper jztAttachmentBusinessMapper;

    @Resource
    private HashIndexMapper hashIndexMapper;

    @Resource
    private NoHashValueJsonMapper noHashValueJsonMapper;

    @Resource
    private MongoTemplate mongoTemplate;

    @Async
    @Override
    public Future<String> addJZTAttachmentBusiness(List<JZTAttachmentBusiness> jztAttachmentBusinesss) {

       try {
           for (int k=0;k<jztAttachmentBusinesss.size();k++){
              /* Date currentDate = new Date();
               SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
               Date createTime=sdf.parse(sdf.format(currentDate));*/
               JZTAttachmentBusiness jztAttachmentBusiness=jztAttachmentBusinesss.get(k);
               /*jztAttachmentBusiness.setCreateTime(createTime);
               jztAttachmentBusiness.setUpdateTime(createTime);*/
               String attachmentStr=jztAttachmentBusiness.getAttachment();
               if (!"".equals(attachmentStr) && attachmentStr != null) {
                   String collectionName = "jztAttachmentBusiness";
                   String imageHashValue = ImagePathHashValueUtil.insertAttachment(attachmentStr, mongoTemplate, collectionName);
                   jztAttachmentBusiness.setAttachment(imageHashValue);
               }
               String hashValueStr= jztAttachmentBusiness.toString();
               //HashIndex hashIndex=HashValueUtil.getHashValue(donateCashStr);
               HashIndex hashIndex=new HashIndex();
               int trials = 3;
               Integer insertChainFlag = 0;
               Integer flag1 = 0;
               Integer flag2 = 0;
               int sleepNum=1000;

               for (int i=0;i<trials;i++){
                   hashIndex= HashValueUtil.getHashValue(hashValueStr);
                   String hashValue=hashIndex.getHashValue();
                   String txId=hashIndex.getTxId();
                   Long blockNum=hashIndex.getBlockNum();
                   if(EmptyUtils.isNotEmpty(hashValue)&&EmptyUtils.isNotEmpty(txId)&&EmptyUtils.isNotEmpty(blockNum)){
                       insertChainFlag = 1;
                       break;
                   }else{
                       sleepNum=sleepNum*i*5;
                       Thread.sleep(sleepNum);
                   }

               }

               if(insertChainFlag > 0) {
                   jztAttachmentBusiness.setHashValue(hashIndex.getHashValue());
                   flag1 = hashIndexMapper.insert(hashIndex);
                   flag2 = jztAttachmentBusinessMapper.addJZTAttachmentBusiness(jztAttachmentBusiness);
                   if (flag1 == 0) {
                       String hashIndexStr = hashIndex.toString();
                   }
                   if (flag2 == 0) {
                       //tianjia database
                       String donateCashStr1 = jztAttachmentBusiness.toString();
                   }
               } else {
                   //tianjia database
                   NoHashValueJson noHashValueJson=new NoHashValueJson();
                   noHashValueJson.setRecordJson(hashValueStr);
                   noHashValueJson.setHashTableName("jzt_attachment_business");
                   noHashValueJsonMapper.insert(noHashValueJson);
               }
           }
       }catch(RuntimeException e){
           e.printStackTrace();
           throw new RuntimeException();
       }catch (Exception e){
           e.printStackTrace();
       }

        return new AsyncResult<String>("添加成功");
    }


    @Override
    public List<JZTAttachmentBusinessVO> getJZTAttachmentBusinessByBusiness(String businessOrderNumber) {
        return jztAttachmentBusinessMapper.getJZTAttachmentBusinessByBusiness(businessOrderNumber);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer recordJZTAttachmentBusiness(String hashValue, HashIndex hashIndex, JZTAttachmentBusiness jztAttachmentBusiness) throws Exception {
        Integer flag1 = hashIndexMapper.insert(hashIndex);
        Integer flag2 = jztAttachmentBusinessMapper.addJZTAttachmentBusiness(jztAttachmentBusiness);
        Integer flag3 = noHashValueJsonMapper.deleteByHashValue(hashValue);
        if (flag1 == 0 || flag2 == 0 || flag3 == 0){
            throw new Exception("record error");
        }
        return 1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer recordJZTAttachmentBusinessBatch(List<String> hashValueList, List<HashIndex> hashIndexList, List<JZTAttachmentBusiness> jztAttachmentBusinessList) throws Exception {
        Integer flag1 = hashIndexMapper.insertBatch(hashIndexList);
        Integer flag2 = jztAttachmentBusinessMapper.insertBatch(jztAttachmentBusinessList);
        Integer flag3 = noHashValueJsonMapper.deleteBatchByHashValue(hashValueList);
        if (flag1 == 0 || flag2 == 0 || flag3 == 0){
            throw new Exception("record error");
        }
        return 1;
    }

    @Override
    public List<JZTAttachmentBusinessVO> grantGetJZTAttachmentBusinessByBusiness(String businessOrderNumber, String userName) {
        return jztAttachmentBusinessMapper.grantGetJZTAttachmentBusinessByBusiness(businessOrderNumber,userName);
    }

    @Override
    public Integer insertBatch(List<JZTAttachmentBusiness> jztAttachmentBusinessList) {
        return jztAttachmentBusinessMapper.insertBatch(jztAttachmentBusinessList);
    }


}
